﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Model;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class YHGLForm : Form
    {
        private UserInfo _user;
        private bool _isSystem = false;
        private string _orgCode = Session.CurrentOrgCode;

        public YHGLForm()
        {
            InitializeComponent();
            InitRoleItems();
            _orgCode = Session.CurrentOrgCode;
        }

        public YHGLForm(UserInfo u)
            : this()
        {
            if (u != null && !string.IsNullOrEmpty(u.ID))
            {
                _user = u;
                this.txtUserName.Text = u.UserName;
                this.txtPwd.Text = u.Password;
                this.txtXM.Text = u.XM;
                this.cbbRole.SelectedValue = u.Role;
                if (u.IsSystem == "1")
                {
                    this.cbbRole.Enabled = false;
                }
            }
        }

        public YHGLForm(bool issys, string code)
            : this()
        {
            _isSystem = issys;
            _orgCode = code;
            this.cbbRole.SelectedValue = "1";
            this.cbbRole.Enabled = false;
        }

        private void InitRoleItems()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("valueExpr", typeof(string));
            dt.Columns.Add("disExpr", typeof(string));
            dt.Rows.Add("0", "普通用户");
            dt.Rows.Add("1", "管理员");

            cbbRole.DataSource = dt;
            cbbRole.ValueMember = "valueExpr";
            cbbRole.DisplayMember = "disExpr";

            cbbRole.SelectedValue = "0";
        }

        private bool CheckInput()
        {
            if (string.IsNullOrWhiteSpace(txtUserName.Text))
            {
                MessageBox.Show("用户名不能为空！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(txtPwd.Text))
            {
                MessageBox.Show("密码不能为空！");
                return false;
            }
            if (cbbRole.SelectedValue == null)
            {
                MessageBox.Show("角色不能为空！");
                return false;
            }
            string strSQL = "select ID from T_SYS_USER where USERNAME = '" + txtUserName.Text + "'";
            if (_user != null && !string.IsNullOrEmpty(_user.ID))//修改
            {
                strSQL += " and ID <> '" + _user.ID + "'";
            }
            DataTable dtUser = DataAccess.ExecuteDataTable(strSQL);
            if (dtUser != null && dtUser.Rows.Count > 0)
            {
                MessageBox.Show("该用户名已存在！");
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                if (_user != null && !string.IsNullOrEmpty(_user.ID))//修改
                {
                    DataAccess.ExecuteNonQuery("update T_SYS_USER set USERNAME='" + txtUserName.Text + "',PASSWORD='" + txtPwd.Text + "',ROLE='" + cbbRole.SelectedValue + "',XM='" + txtXM.Text + "' where ID = '" + _user.ID + "'");
                }
                else//新增
                {
                    DataAccess.ExecuteNonQuery("insert into T_SYS_USER(ID, USERNAME, PASSWORD, ROLE, ORGCODE, XM, ISSYSTEM) " +
                        "values('" + Guid.NewGuid().ToString() + "','" + txtUserName.Text + "','" + txtPwd.Text + "','" + cbbRole.SelectedValue + "','" + _orgCode + "','" + txtXM.Text + "','" + IsSystemStr + "')");
                }
                this.DialogResult = DialogResult.OK;
            }
        }

        private string IsSystemStr
        {
            get
            {
                return _isSystem ? "1" : "0";
            }
        }
    }
}
